package com.aca.market.service.impl;

import com.aca.market.entity.Role;
import com.aca.market.entity.RolePerm;
import com.aca.market.mapper.RoleMapper;
import com.aca.market.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author aca
 * @since 2020-11-13
 */
@Transactional
@Service
@Slf4j
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Override
    //由permListId生成对应实体 加入数据库
    public List<RolePerm> convertToRolePerm(List<Long> permIdList, Long roleId){
        return permIdList.parallelStream()
                        .map((p -> new RolePerm()
                        .setPermissionId(p)
                        .setRoleId(roleId)))
                        .collect(Collectors.toList());
    }

    @Override
    public Set<String> getSubjectRoles(List<String> permissions){
        Set<String> roles = new HashSet<>();
        for (String s : permissions) {
            String role = s.split(":")[0];
            if(!roles.contains(role)){
                roles.add(role);
                log.info("====================getRole " + role);
            }
        }
        return roles;
    }

}
